package com.xquant.pricing.systerm.controller;

import com.github.pagehelper.PageInfo;
import com.xquant.common.engine.xPP.cxf.NotifyDataFacade;
import com.xquant.common.log.annotation.Log;
import com.xquant.common.log.enums.BusinessType;
import com.xquant.pricing.calc.entity.Task;
import com.xquant.pricing.calc.entity.TaskLog;
import com.xquant.pricing.systerm.service.TaskService;
import com.xquant.pricing.utils.CacheUtil;
import com.xquant.pricing.utils.CommResponse;
import com.xquant.common.engine.xPP.cxf.webservice.RunTaskResponse;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

import static com.xquant.common.core.utils.PageUtils.startPage;

@RestController
@RequestMapping("task")
public class TaskController {

  @Autowired private TaskService taskService;
  @Autowired
  private NotifyDataFacade notifyDataFacade;

  @RequestMapping(value = "/queryTaskListByPage.action")
  @PreAuthorize("@ss.hasPermi('pricing:task:list')")
  public PageInfo<Task> queryTaskListByPage(@RequestParam Map<String, Object> param) {
    startPage();
    return taskService.queryTaskListByPage(param);
  }

  @RequestMapping(value = "/queryTaskLogListByPage.action")
  @Log(title = "定时任务配置", businessType = BusinessType.QUOERY)
  @PreAuthorize("@ss.hasPermi('pricing:taskLog:list')")
  public PageInfo<TaskLog> queryTaskLogListByPage(@RequestParam Map<String, Object> param) {
    startPage();
    return taskService.queryTaskLogListByPage(param);
  }

  @RequestMapping(value = "/addTask.action")
  @Log(title = "定时任务配置", businessType = BusinessType.INSERT)
  @PreAuthorize("@ss.hasPermi('pricing:task:add')")
  public CommResponse addTask(@RequestParam Map<String, Object> param) {
    Task task = new Task();
    task.setName(MapUtils.getString(param, "name"));
    task.setType(MapUtils.getString(param, "type"));
    task.setContext(MapUtils.getString(param, "context"));
    task.setOffsetDays(MapUtils.getInteger(param, "offsetDays"));
    task.setCronExpression(MapUtils.getString(param, "cronExpression"));
    task.setStatus(0);
    taskService.addTask(task);
    return CommResponse.success("保存成功");
  }

  @RequestMapping(value = "/updateTask.action")
  @Log(title = "定时任务配置", businessType = BusinessType.UPDATE)
  @PreAuthorize("@ss.hasPermi('pricing:task:update')")
  public CommResponse updateTask(@RequestParam Map<String, Object> param) {
    Task task = new Task();
    task.setId(MapUtils.getInteger(param, "id"));
    task.setName(MapUtils.getString(param, "name"));
    task.setType(MapUtils.getString(param, "type"));
    task.setContext(MapUtils.getString(param, "context"));
    task.setOffsetDays(MapUtils.getInteger(param, "offsetDays"));
    task.setCronExpression(MapUtils.getString(param, "cronExpression"));
    task.setStatus(MapUtils.getInteger(param, "status"));
    taskService.updateTask(task);
    return CommResponse.success("保存成功");
  }

  @RequestMapping(value = "/deleteTask.action")
  @Log(title = "定时任务配置", businessType = BusinessType.DELETE)
  @PreAuthorize("@ss.hasPermi('pricing:task:delete')")
  public CommResponse deleteTask(@RequestParam Map<String, Object> param) {
    Task task = new Task();
    task.setId(MapUtils.getInteger(param, "id"));
    task.setType(MapUtils.getString(param, "type"));
    taskService.deleteTask(task);
    return CommResponse.success("删除成功");
  }

  @RequestMapping(value = "/activeTask.action")
  @PreAuthorize("@ss.hasPermi('pricing:task:start')")
  public CommResponse activeTask(@RequestParam Map<String, Object> param) {
    Task task = new Task();
    task.setId(MapUtils.getInteger(param, "id"));
    task.setName(MapUtils.getString(param, "name"));
    task.setType(MapUtils.getString(param, "type"));
    task.setContext(MapUtils.getString(param, "context"));
    task.setOffsetDays(MapUtils.getInteger(param, "offsetDays"));
    task.setCronExpression(MapUtils.getString(param, "cronExpression"));
    task.setStatus(1);
    taskService.updateTask(task);
    return CommResponse.success("启用成功");
  }

  @RequestMapping(value = "/suspendTask.action")
  @PreAuthorize("@ss.hasPermi('pricing:task:stop')")
  public CommResponse suspendTask(@RequestParam Map<String, Object> param) {
    Task task = new Task();
    task.setId(MapUtils.getInteger(param, "id"));
    task.setName(MapUtils.getString(param, "name"));
    task.setType(MapUtils.getString(param, "type"));
    task.setContext(MapUtils.getString(param, "context"));
    task.setOffsetDays(MapUtils.getInteger(param, "offsetDays"));
    task.setCronExpression(MapUtils.getString(param, "cronExpression"));
    task.setStatus(2);
    taskService.updateTask(task);
    return CommResponse.success("停用成功");
  }

  @RequestMapping(value = "/executeTask.action")
  @Log(title = "定时任务配置", businessType = BusinessType.OTHER)
  @PreAuthorize("@ss.hasPermi('pricing:task:execute')")
  public CommResponse executeTask(@RequestParam Map<String, Object> param) {
    //1、通知行情变化
    notifyDataFacade.notifyDailySet();
    CacheUtil.getInstance().removeAllCacheData();

    Task task = new Task();
    task.setId(MapUtils.getInteger(param, "id"));
    task.setName(MapUtils.getString(param, "name"));
    task.setType(MapUtils.getString(param, "type"));
    task.setContext(MapUtils.getString(param, "context"));
    task.setBegDate(MapUtils.getString(param, "executeBegDate"));
    task.setEndDate(MapUtils.getString(param, "executeEndDate"));
    task.setOffsetDays(MapUtils.getInteger(param, "offsetDays"));
    task.setCronExpression(MapUtils.getString(param, "cron"));
    taskService.executeTask(task);
    return CommResponse.success("执行发起成功");
  }

  @Log(title = "定时任务配置", businessType = BusinessType.OTHER)
  @RequestMapping(value = "/executeSyncTask.action")
  public CommResponse executeSyncTask(@RequestParam Map<String, Object> param) {
    Task task = new Task();
    task.setId(MapUtils.getInteger(param, "id"));
    task.setName(MapUtils.getString(param, "name"));
    task.setType(MapUtils.getString(param, "type"));
    task.setContext(MapUtils.getString(param, "context"));
    task.setBegDate(MapUtils.getString(param, "executeBegDate"));
    task.setEndDate(MapUtils.getString(param, "executeEndDate"));
    task.setOffsetDays(MapUtils.getInteger(param, "offsetDays"));
    task.setCronExpression(MapUtils.getString(param, "cron"));
    RunTaskResponse ret = taskService.executeSyncTask(task);
    if (ret != null && ret.getRunTaskResult().getErrorCode() >= 0) {
      return CommResponse.success();
    } else {
      return CommResponse.failure(ret == null ? "操作失败！" : ret.getRunTaskResult().getErrorMsg());
    }
  }
}
